package com.stock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.stock.StockProp;

public class StockInfoDao {
	public static void updateStockInfo(StockProp stockEntity) {
		try {
			DbConn conn = ConnectFactory.getConnectionInstance(JDBC.getJdbc());
			if (findStockByCode(stockEntity.getCode()) != null) {
				return;
			}
			StringBuffer sb = new StringBuffer("INSERT INTO stock_info(stock_code,stock_name,status,stock_info,remark) VALUES(");
			sb.append("'"+stockEntity.getCode()+"',")
			  .append("'"+stockEntity.getName()+"',")
			  .append("1,")
			  .append("'',")
			  .append("'')");
			System.out.println(sb.toString()+"begin");
			conn.executeUpdate(sb.toString());
			System.out.println(sb.toString()+"over");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static List<StockProp> queryStocks() {
		List<StockProp> rtnList = new ArrayList<StockProp>();
		try {
			DbConn conn = ConnectFactory.getConnectionInstance(JDBC.getJdbc());
			List<Map> dataList = conn.queryData("select * from stock_info order by stock_id asc");
			for (Map m : dataList) {
				StockProp sp = new StockProp();
				sp.setStockId(Integer.parseInt(m.get("stock_id").toString()));
				sp.setCode(m.get("stock_code").toString());
				rtnList.add(sp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtnList;
	}
	
	public static StockProp findStockByCode(String code) {
		try {
			DbConn conn = ConnectFactory.getConnectionInstance(JDBC.getJdbc());
			List<Map> dataList = conn.queryData("select * from stock_info where stock_code='"+code+"'");
			if (dataList.size() > 0) {
				Map m = dataList.get(0);
				StockProp sp = new StockProp();
				sp.setStockId(Integer.parseInt(m.get("stock_id").toString()));
				sp.setCode(m.get("stock_code").toString());
				return sp;
			} else {
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
